package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.customview.img.StkWatermarkImgView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.WaterColorAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.databinding.ActivityPicWaterMarkBinding;
import flc.ast.utils.Content;
import flc.ast.utils.MyWaterMark;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class PicWaterMarkActivity extends BaseAc<ActivityPicWaterMarkBinding> {

    public static String imgPath = "";
    private WaterColorAdapter colorAdapter;
    private int oldColorPos = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_water_mark;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicWaterMarkBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvPicWaterMarkSave.setOnClickListener(this);
        mDataBinding.tvPicWaterMarkKind1.setOnClickListener(this);
        mDataBinding.tvPicWaterMarkKind2.setOnClickListener(this);
        mDataBinding.tvPicWaterMarkKind3.setOnClickListener(this);
        mDataBinding.rvColorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        colorAdapter = new WaterColorAdapter();
        mDataBinding.rvColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPicWaterMarkSave://保存
                saveImg();
                break;
            case R.id.tvPicWaterMarkKind1://左倾斜
                clearView();
                mDataBinding.tvPicWaterMarkKind1.setBackgroundResource(R.drawable.shape_water_mark_sel);
                mDataBinding.tvPicWaterMarkKind1.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.watermarkImg.setStyle(StkWatermarkImgView.Style.ZUO_XIE);
                break;
            case R.id.tvPicWaterMarkKind2://水平
                clearView();
                mDataBinding.tvPicWaterMarkKind2.setBackgroundResource(R.drawable.shape_water_mark_sel);
                mDataBinding.tvPicWaterMarkKind2.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.watermarkImg.setStyle(StkWatermarkImgView.Style.HORIZONTAL);
                break;
            case R.id.tvPicWaterMarkKind3://垂直
                clearView();
                mDataBinding.tvPicWaterMarkKind3.setBackgroundResource(R.drawable.shape_water_mark_sel);
                mDataBinding.tvPicWaterMarkKind3.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.watermarkImg.setStyle(StkWatermarkImgView.Style.VERTICAL);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        colorAdapter.getItem(oldColorPos).setSelect(false);
        colorAdapter.notifyItemChanged(oldColorPos);
        oldColorPos = position;
        colorAdapter.getItem(position).setSelect(true);
        colorAdapter.notifyItemChanged(position);
        mDataBinding.sbPicWaterMarkTran.setProgress(90);
        mDataBinding.watermarkImg.setTextColor(Color.parseColor(colorAdapter.getItem(position).getColor()));
    }


    @Override
    protected void initData() {
        if (TextUtils.isEmpty(imgPath)) return;
        Glide.with(this).load(imgPath).into(mDataBinding.watermarkImg);
        //颜色数据
        List<MyColorBean> listColor = new ArrayList<>();
        listColor.add(new MyColorBean("#000000", true));
        listColor.add(new MyColorBean("#FFFFFF", false));
        listColor.add(new MyColorBean("#E12D2D", false));
        listColor.add(new MyColorBean("#FFA8A8", false));
        listColor.add(new MyColorBean("#FFB652", false));
        listColor.add(new MyColorBean("#FFF62C", false));
        listColor.add(new MyColorBean("#79BA56", false));
        listColor.add(new MyColorBean("#35B9B0", false));
        listColor.add(new MyColorBean("#3F68BC", false));
        listColor.add(new MyColorBean("#9F45F1", false));
        listColor.add(new MyColorBean("#E85C90", false));
        listColor.add(new MyColorBean("#C0C0C0", false));
        listColor.add(new MyColorBean("#CA58C9", false));
        listColor.add(new MyColorBean("#8A72E1", false));
        listColor.add(new MyColorBean("#46D5AF", false));
        listColor.add(new MyColorBean("#5E8C4F", false));
        colorAdapter.setList(listColor);
        mDataBinding.watermarkImg.setTextColor(Color.parseColor(colorAdapter.getItem(0).getColor()));
        //水印内容
        mDataBinding.etPicWaterMark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mDataBinding.watermarkImg.setWatermarkText(s.toString());
            }
        });
        //水印透明度
        mDataBinding.sbPicWaterMarkTran.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mDataBinding.watermarkImg.setTextAlpha(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //字体大小
        mDataBinding.sbPicWaterMarkSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mDataBinding.watermarkImg.setTextSize(SizeUtils.sp2px(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.watermarkImg);
                ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                String resultFile = FileUtil.generateFilePath(Content.myCreate, ".png");
                ImageUtils.save(bitmap, resultFile, Bitmap.CompressFormat.PNG);
                emitter.onNext(resultFile);
            }

            @Override
            public void accept(String path) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissDialog();
                        PicWaterMarkResultActivity.imgPath = path;
                        startActivity(PicWaterMarkResultActivity.class);
                    }
                }, 500);
            }
        });
    }


    //管理kind选择
    private void clearView() {
        mDataBinding.tvPicWaterMarkKind1.setBackgroundResource(R.drawable.shape_water_mark_bg);
        mDataBinding.tvPicWaterMarkKind2.setBackgroundResource(R.drawable.shape_water_mark_bg);
        mDataBinding.tvPicWaterMarkKind3.setBackgroundResource(R.drawable.shape_water_mark_bg);
        mDataBinding.tvPicWaterMarkKind1.setTextColor(Color.parseColor("#8F8F8F"));
        mDataBinding.tvPicWaterMarkKind2.setTextColor(Color.parseColor("#8F8F8F"));
        mDataBinding.tvPicWaterMarkKind3.setTextColor(Color.parseColor("#8F8F8F"));
    }

}